//
//  MCFreightCell.swift
//  StoreManage
//
//  Created by MC on 2018/9/20.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

typealias MCFreightCellClosure = () -> Void

typealias MCFreightCell_editClosure = (MCFreightModel) -> Void


class MCFreightCell: UITableViewCell {

    var closure : MCFreightCellClosure?
    var editClosure : MCFreightCell_editClosure?

    
    var model = MCFreightModel() {
        didSet {
            let freight = MCString_judgemantDecimalsIsZero(figure: model.defaultFreight.intValue)
            let str = model.name + "(默认运费\(freight))"
            
            nameLabel.attributedText = str.MCAttributedString_color(color: UIColor.MCWords, range: NSRange.init(location: 0, length: model.name.count))
            
            
            statusButton.isSelected = model.b
            
            var height: CGFloat = 0
            
            if model.nonDistributionList.count == 0 {
                hateButton.nameLabel.text = "全国配送"
                hateButton.iconImageView.image = nil
                model.isShow = false
            } else {
                hateButton.nameLabel.text = "不配送区域"
                if model.isShow {
                    hateButton.iconImageView.image = #imageLiteral(resourceName: "Reuse_arrow_top")
                    model.isShow = true
                } else {
                    hateButton.iconImageView.image = #imageLiteral(resourceName: "Reuse_arrow_bottom")
                    model.isShow = false
                }
            }
            
        
            
            for itemModel in model.nonDistributionModels {
                let itemHeight = itemModel.allCitysHeight
                
                let view = MCFreightCellContentView()
                
                view.isHidden = !model.isShow
                
                view.provinceLabel.text = itemModel.proName
                view.citysLabel.text = itemModel.allCitysStr
                
                bgView.addSubview(view)
                view.snp.remakeConstraints { (make) ->Void in
                    make.left.equalTo(nameLabel)
                    make.right.equalTo(-10)
                    make.top.equalTo(lineView.snp.bottom).offset(10 + height)
                    make.height.equalTo(itemHeight)
                }
                
                height += (itemHeight + 10)
            }
        }
    }
    
    
    
    public class func customCell(tableView:UITableView) -> MCFreightCell {
        
        let reuseIdentifier = "cell";
        var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
        
        cell = MCFreightCell(style: .default, reuseIdentifier: reuseIdentifier)
        
        return cell as! MCFreightCell
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = UIColor.MCGray_light
        self.selectionStyle = UITableViewCellSelectionStyle.none
        
        self.addSubview(bgView)
        bgView.addSubview(nameLabel)
        bgView.addSubview(statusButton)
        bgView.addSubview(editButton)
        bgView.addSubview(hateButton)
        bgView.addSubview(lineView)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()

        
        bgView.snp.remakeConstraints { (make) ->Void in
            make.edges.equalTo(self).inset(UIEdgeInsetsMake(0, 0, 7, 0))
        }
        
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(bgView).offset(50)
            make.right.equalTo(bgView).offset(-50)
            make.top.equalTo(15)
            make.height.equalTo(40)
        }
        
        
        statusButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.centerY.equalTo(nameLabel)
            make.width.height.equalTo(20)
        }
        
        editButton.snp.remakeConstraints { (make) ->Void in
            make.right.equalTo(-5)
            make.centerY.equalTo(nameLabel)
            make.width.height.equalTo(40)
        }
        
        hateButton.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(bgView).offset(0)
            make.top.equalTo(nameLabel.snp.bottom)
            make.size.equalTo(CGSize.init(width: 120, height: 34))
        }
        
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(bgView)
            make.height.equalTo(1)
            make.top.equalTo(hateButton.snp.bottom)
        }

        
    }
    
    @objc func statusButtonClicked() {
//        statusButton.isSelected = !statusButton.isSelected
    }
    
    @objc func showDataEvent() {
       
        model.isShow = !model.isShow
        
        closure?()
    }
    
    @objc func editButtonClicked(){
        
        //:TODO
        editClosure?(model)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    }()
    
    lazy var statusButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Reuse_selected"), for: .normal)
        button.setImage(#imageLiteral(resourceName: "Reuse_selectedSign"), for: .selected)
        button.addTarget(self, action: #selector(statusButtonClicked), for: .touchUpInside)
        return button
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.MCMain
        label.numberOfLines = 0
        return label
    }()
    
    lazy var editButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.setImage(#imageLiteral(resourceName: "Mine_edit").MCImageWithTintColor(color: UIColor.MCGray_middle), for: .normal)
        button.addTarget(self, action: #selector(editButtonClicked), for: .touchUpInside)

        return button
    }()
    
    lazy var hateButton: MCReverseHorizontalButton = {
        let button = MCReverseHorizontalButton()
        button.nameLabel.text = "不配送区域"
        button.nameLabel.textColor = UIColor.MCWords
        button.backgroundColor = UIColor.white
        button.iconImageView.image = #imageLiteral(resourceName: "Reuse_arrow_bottom")
        button.addTarget(self, action: #selector(showDataEvent), for: .touchUpInside)

        return button
    }()
    
//    lazy var hateButton: UIButton = {
//        let button = UIButton.init(type: UIButtonType.custom)
//        button.titleLabel?.font = MCFont(14)
//        button.setTitle("不配送区域", for: .normal)
//        button.setTitle("全国配送", for: .selected)
//        button.setTitleColor(UIColor.MCWords, for: .normal)
//        button.addTarget(self, action: #selector(showDataEvent), for: .touchUpInside)
//        return button
//    }()
//
//    lazy var arrowButton: UIButton = {
//        let button = UIButton.init(type: UIButtonType.custom)
//        button.setImage(#imageLiteral(resourceName: "Reuse_arrow_bottom"), for: .normal)
//        button.setImage(#imageLiteral(resourceName: "Reuse_arrow_top"), for: .selected)
//        button.addTarget(self, action: #selector(showDataEvent), for: .touchUpInside)
//        return button
//    }()

        lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
}


class MCFreightCellContentView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        self.addSubview(provinceLabel)
        self.addSubview(citysLabel)
        self.addSubview(lineView)

        provinceLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(0)
            make.top.equalTo(self)
        }


        citysLabel.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(0)
            make.top.equalTo(provinceLabel.snp.bottom).offset(5)
        }


        
        lineView.snp.remakeConstraints { (make) ->Void in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(1)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var provinceLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(14)
        label.textColor = UIColor.MCWords
        return label
    }()

    lazy var citysLabel: UILabel = {
        let label = UILabel()
        label.font = MCFont(12)
        label.textColor = UIColor.MCGray_middle
        label.numberOfLines = 0
        return label
    }()
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
}
